/**
 * T extends { length: number }
 *    约束 T 必须要有一个 number 类型的 length 属性
 */
function identity<T extends { length: number }>(value: T): T {
  console.log(value.length);
  return value;
}

identity(["1", "2"]);

interface Person {
  name: string;
  age: number;
  location: string;
}

/**
 * keyof 操作符
 *    语法
 *        type 新类型 = keyof 类型
 */
type k1 = keyof { name: string; age: number; location: Location }; // "name" | "age" | "location"
type k2 = keyof Person; // "name" | "age" | "location"
type k3 = keyof { [x: string]: Person }; // string | number

function getProperty<T, K extends keyof T>(obj: T, key: K) {
  return obj[key];
}

getProperty({ name: "张三", age: 18, sex: "男", gender: 0 }, "gender");
export {};
